package com.sql.connection;

import org.junit.Test;

import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.*;

public class DriverManagerDemo {
    /**
     * 尝试连接数据库
     * @param args
     */
    public static void main(String[] args) {
        Connection con = null;
        try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://47.99.209.115:3306/test","root","pass");
        Statement st = con.createStatement();
        String sql = "select * from SC ";
        ResultSet rs = st.executeQuery(sql);

            while(rs.next()){
                System.out.println(rs.getString(1));
                System.out.println(rs.getString("Cid"));
                System.out.println();
        }

        if (rs != null) rs.close();
        if (st != null) st.close();
        if (con != null) con.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

        }
    }

    /**
     * 打开 jdbc日志 copyonwritearraylistdemo
     */
    @Test
    public void testOpenLog(){
        try {
//            DriverManager.setLogWriter(new PrintWriter(System.out));
//            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            Connection con = DriverManager.getConnection("jdbc:mysql://47.99.209.115:3306/test","root","306319");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
